<?php
ini_set('display_errors', 1);
error_reporting(-1);
ini_set('memory_limit', '1024M');
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\brandAccount\BrandAccountInfo as mBrandAccountInfo;

use Biz\model\brandAccount\BrandAccountAuth as mBrandAccountAuth;

use Doraemon\tools\Sms;

$page = 1;

$pageSize = 100;

$where = [
    'type' => mBrandAccountInfo::ACCOUNT_TYPE_BRAND,
    'account_status' => mBrandAccountInfo::ACCOUNT_STATUS_USED,
    'in_end_time[>=]' => date('Y-m-d', time()),
];
$where['ORDER'] = ['id' => 'ASC'];

do {
    $limit = [($page - 1) * $pageSize, $pageSize];
    $where['LIMIT'] = $limit;
    $accountList = mBrandAccountInfo::getAllByWhere($where);

    $accountIds = array_column($accountList, 'id');

    $accountAuths = mBrandAccountAuth::getAllByWhere([
        'account_id' => $accountIds,
        'auth_status' => mBrandAccountAuth::AUTH_STATUS_USED
    ]);

    $accountAuthPhones = empty($accountAuths) ? [] : array_column($accountAuths, 'phone', 'account_id');

    foreach ($accountList as $account) {
        $phone = $accountAuthPhones[$account['id']] ?? '';
        if (empty($phone) || strlen($phone) != 11 || strpos($phone, '142') === 0) {
            continue;
        }
        echo "$phone" . PHP_EOL;
        send($phone, []);
    }
    $page++;
} while (!empty($accountList));


function send(string $phone, array $aParam)
{

    $aAliyunTemp = ['aliyun' => ['template' => Sms::TYPE_REMIND_BRAND_LEVEL_ONLINE]];
    $result = Sms::sendTplMsg($phone, $aAliyunTemp, $aParam, '86');

    if (!$result) {
        echo "fail:$phone" . PHP_EOL;
    }

    return $result;
}